package com.happyfamily.springboot.crowdsourcingplatform.controller.sun;


import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.*;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.SysUserService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.NeedService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.TenderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api-front/tender/")
public class TenderController
{
	@Autowired
	private TenderService tenderServiceImpl;

	@Autowired
	private NeedService needServiceImpl;

	@Autowired
	private SysUserService userServiceImpl;

	@RequestMapping("getList")
	@ResponseBody
	@ApiOperation(value = "服务商获取所有符合条件的需求", notes = "服务商获取所有符合条件的需求", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商获取所有符合条件的需求", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public String list(HttpServletRequest request, String limit, String page, String acc, String type) throws Exception
	{
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		SysUser sysUser = userServiceImpl.getUser(account);
		Integer li = Integer.valueOf(limit);
		Integer pa = Integer.valueOf(page);
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(needServiceImpl.getSum(acc,type));
		List<Need> list = needServiceImpl.getList(li, pa, acc,type);
		for (Need need : list)
		{
			String str = tenderServiceImpl.checkTender(sysUser.getUid(), need.getNid());
			if (str.equals("yes"))
			{
				need.setTips3("未投标");
			} else
			{
				if (str.equals("个人"))
				{
					need.setTips3("已个人投标");
				} else
				{
					need.setTips3("已联盟投标");
					need.setTips2(str);
				}
			}
		}
		tableModel.setData(list);
		Gson g = new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("addTender")
	@ResponseBody
	@ApiOperation(value = "服务商对某需求进行投标", notes = "服务商对某需求进行投标", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商对某需求进行投标", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.INSERT)
	public String addTender(String nid, String aid, String ttype, HttpServletRequest request)
	{
		String str = "no";
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		SysUser sysUser = userServiceImpl.getUser(account);
		String today=new SimpleDateFormat("YYYY-MM-dd").format(new Date());
		Need need=needServiceImpl.getNeed(Long.valueOf(nid));
		if(today.compareTo(need.getNendtime())<=0)
		{
			Tender tender = new Tender();
			tender.setNeedid(Long.valueOf(nid));
			tender.setTtype(ttype);
			tender.setTstarttime(today);
			if (ttype.equals("个人投标"))
			{
				tender.setUserid(sysUser.getUid());
			} else
			{
				tender.setUserid(Long.valueOf(aid));
			}
			boolean flag = tenderServiceImpl.addTender(tender);
			if (flag)
			{
				str = "yes";
			}
		}
		return str;
	}

	@RequestMapping("deleteTender")
	@ResponseBody
	@ApiOperation(value = "服务商对某需求取消投标", notes = "服务商对某需求取消投标", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商对某需求取消投标", level = 5, operationUnit = OperationUnit.USER, operationType = OperationType.ALL)
	public String deleteTender(HttpServletRequest request, String nid, String aid, String type)
	{
		String str = "no";
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		SysUser sysUser = userServiceImpl.getUser(account);

		if (type.equals("已个人投标"))
		{
			Tender tender = tenderServiceImpl.getTender(sysUser.getUid(), Long.valueOf(nid));
			if (tender.getTendtime() == null)
			{
				boolean flag = tenderServiceImpl.deleteTende(sysUser.getUid(), Long.valueOf(nid));
				if (flag)
				{
					str = "yes";
				}
			}
		} else
		{
			Tender tender = tenderServiceImpl.getTender(Long.valueOf(aid), Long.valueOf(nid));
			if (tender.getTendtime() == null)
			{
				boolean flag = tenderServiceImpl.deleteTende(Long.valueOf(aid), Long.valueOf(nid));
				if (flag)
				{
					str = "yes";
				}
			}
		}

		return str;
	}
}